package com.example.demo.web.controller;

import com.example.demo.bean.Application;

import com.example.demo.service.IApplicationService;
import com.example.demo.utils.ExcelUtils;
import com.example.demo.utils.Message;
import com.example.demo.utils.MessageUtil;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/application")
public class ApplicationController {
    @Autowired
    private IApplicationService applicationService;
    @ApiOperation(value = "导入到Excel中",notes =  "注意！测试的时候请将地址粘贴到浏览器地址栏测试", produces = "application/octet-stream")
    @GetMapping("download")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name",value="应用名称",paramType = "query"),
    })
    public void  download(HttpServletResponse response,Long id,String name) throws Exception{
        //1.查询数据
        List<Application> list =applicationService.query(id,name);
        //2.将数据填充excel导出
        //2.1 excel头部数据
        String[] headList = new String[] {"应用名称","类型","应用介绍","邮箱","电话","经度"," 纬度"};
        //2.2 属性
        String[] fileList =new String[] {"name","type","introduce","email","telephone","longitude","latitude"};
        //2.3 数据列，需要转化
        List<Map<String,Object>> dataList =new ArrayList<>();
        for (Application app : list){
            //每一个map相当于是一行数据，一行数据中多列
            Map<String,Object> map =new HashMap<>();
            map.put("name", app.getName());
            map.put("type",app.getType());
            map.put("introduce",app.getIntroduce());
            map.put("email",app.getEmail());
            map.put("telephone",app.getTelephone());
            map.put("longitude",app.getLongitude());
            map.put("latitude",app.getLatitude());
            //dataList表示所有行
            dataList.add(map);
        }
        //2.4 导出文件名
        String excelName ="applications";
        //3. 调用工具方法导出Excel
        ExcelUtils.createExcel(response,excelName,headList,fileList,dataList);
    }

    @GetMapping("query")
    public Message query(Long id,String name){
        List<Application> list=applicationService.query(id,name);
        return MessageUtil.success(list);
    }
    @PostMapping("saveOrUpdate")
    public Message saveOrUpdate(Application application){
        applicationService.saveOrUpdate(application);
        return MessageUtil.success("操作成功");
    }
   @DeleteMapping("deleteById")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "应用编号", required = true, paramType = "query"),
    })
   public Message deleteById( @NotNull Long id){
       applicationService.deleteById(id);
       return MessageUtil.success("操作成功");
   }
}
